import java.util.*;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ChinatorHandler extends DefaultHandler {
  
  private List<ChinatorMarker> chinators;
  private ChinatorMarker chinatorActual;
  private StringBuilder sbText;
  
  public List<ChinatorMarker> getChinators(){
    return chinators;
  }
  
  @Override
  public void startDocument() throws SAXException {
    super.startDocument();
    chinators = new ArrayList<ChinatorMarker>();
    sbText = new StringBuilder();  
  }
   @Override
    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, name, attributes);

        if (localName.equals("address_component")) {
            chinatorActual = new ChinatorMarker(name, 0, 0);
        }
    }
        
    @Override
    public void characters(char[] ch, int start, int length)
                   throws SAXException {
 
        super.characters(ch, start, length);
 
        if (this.chinatorActual != null)
            sbText.append(ch, start, length);
    }
        
    @Override
    public void endElement(String uri, String localName, String name)
                   throws SAXException {
 
        super.endElement(uri, localName, name);
        
        if (this.chinatorActual != null) {
 
            if (localName.equals("short_name")) {
                chinatorActual.setName(sbText.toString());
            } 
             
        }
        if (localName.equals("address_component")) {
            chinators.add(chinatorActual);
        }
 
            sbText.setLength(0);
        }
    }
